package com.derive.education_service.web.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.derive.education_service.common.global.ResponseMessage;
import com.derive.education_service.entity.ContentResources;
import com.derive.education_service.mapper.ContentResourcesMapper;
import com.derive.education_service.utils.MyFileUtils;
import com.derive.education_service.web.content.service.ContentResService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 业务内容资源操作接口
 */
@Service
@Slf4j
public class ContentResServiceImpl implements ContentResService {

    @Autowired
    private ContentResourcesMapper contentResourcesMapper;

    @Autowired
    private MyFileUtils myFileUtils;

    /**
     * 添加内容->资源 -- 多文件上传
     * @param files
     * @param contentId
     * @return
     */
    @Override
    public ResponseMessage addContentRes(List<MultipartFile> files, int contentId) {
        // 检查是否上传了资源
        if (files == null || files.isEmpty() || files.size() == 0) {
            // 没有上传资源 --- 还添加个鬼
            return ResponseMessage.createByErrorMessage("没有上传文件{[]}");
        }
        // 有上传，那就循环遍历
        try {
            for (MultipartFile f : files) {
                ResponseMessage ar = myFileUtils.uploadFile(f, "contentRes");
                if (ar.getStatus() == 1) {
                    log.error("文件上传异常{[]}");
                    return ar;
                }
                // 添加数据和资源
                ContentResources contentResources = new ContentResources();
                contentResources.setContentId(contentId);
                contentResources.setCrUrl(String.valueOf(ar.getData()));
                contentResources.setUpdateTime(new Date());

                int row = contentResourcesMapper.insert(contentResources);

                if (row <= 0) {
                    return ResponseMessage.createByErrorMessage("内容资源入库失败");
                }
            }
            return ResponseMessage.createBySuccess("上传成功");
        } catch (IOException e) {
            log.error("文件上传IO异常{[]}");
            e.printStackTrace();
            return ResponseMessage.createByErrorMessage("文件上传IO异常");
        }
    }

    /**
     * 删除内容->资源 -- 删除文件
     * @param crId
     * @return
     */
    @Override
    public ResponseMessage delContentRes(int crId) {
        // 删除单个文件 -- 获取文件的地址
        String url = contentResourcesMapper.selectById(crId).getCrUrl();
        // 删除资源
        ResponseMessage dr = myFileUtils.delFile(url);
        if (dr.getStatus() == 1) {
            // 失败
            return dr;
        }
        // 成功 -- 删除记录
        int row = contentResourcesMapper.deleteById(crId);
        if (row <= 0) {
            // 失败
            return ResponseMessage.createByErrorMessage("内容资源记录删除失败");
        } else {
            return ResponseMessage.createBySuccessMessage("删除成功");
        }
    }

    /**
     * 更改内容资源信息 -- 更新之前先删除原先的资源信息
     * @param file
     * @param crId
     * @return
     */
    @Override
    public ResponseMessage upContentRes(MultipartFile file, int crId) {
        // 校验是否有文件更新
        if (file == null || file.isEmpty()) {
            // 没有上传资源 --- 还添加个鬼
            return ResponseMessage.createByErrorMessage("没有上传文件{[]}");
        }
        // 有文件更新 -- 先删除资源
        String url = contentResourcesMapper.selectById(crId).getCrUrl();
        // 删除资源
        ResponseMessage dr = myFileUtils.delFile(url);
        if (dr.getStatus() == 1) {
            // 失败
            return dr;
        }
        // 上传新文件
        try {
            ResponseMessage ar = myFileUtils.uploadFile(file, "contentRes");
            if (ar.getStatus() == 1) {
                log.error("文件上传异常{[]}");
                return ar;
            }
            // 添加数据和资源
            ContentResources contentResources = new ContentResources();
            contentResources.setCrId(crId);
            contentResources.setCrUrl(String.valueOf(ar.getData()));
            contentResources.setUpdateTime(new Date());

            int row = contentResourcesMapper.insert(contentResources);

            if (row <= 0) {
                return ResponseMessage.createByErrorMessage("内容资源入库失败");
            } else {
                return ResponseMessage.createBySuccessMessage("更新成功");
            }
        } catch (IOException e) {
            log.error("文件上传IO异常{[]}");
            e.printStackTrace();
            return ResponseMessage.createByErrorMessage("文件上传IO异常");
        }
    }

    /**
     * 查询内容下的资源 -- 不分页 -- 只有资源信息
     * @param contentId
     * @return
     */
    @Override
    public ResponseMessage getContentResList(int contentId) {
        QueryWrapper<ContentResources> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("content_id", contentId);
        List<ContentResources> list = contentResourcesMapper.selectList(queryWrapper);

        return ResponseMessage.createBySuccess(list);
    }
}
